<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
        <title>AUI快速完成布局</title>
        <link rel="stylesheet" type="text/css" href="../../css/w3.css" />
        <link rel="stylesheet" type="text/css" href="../../css/aui.2.0.css" />
        <style type="text/css">
            body {
                background: #ffffff;
            }
            .bg-dark {
                background: #333333 !important;
            }
            .aui-bar-btn-item {
                background: #ffffff;
                border: none;
                border-bottom: 2px solid #ffffff;
            }
            .aui-bar-btn-item.aui-active {
                color: #039be5;
                background: #ffffff;
                border-bottom: 2px solid #039be5;
            }
            .nomore-tag {
                margin: 0 auto;
                text-align: center;
                width: auto;
                color: #333;
                padding: 20px 10px 10px 10px;
                font-size: 15px;
            }
            .title-bar {
                margin: 0 auto;
                text-align: left;
                width: auto;
                color: #333;
                padding: 3px 3px 2px 15px;
                font-size: 15px;
            }
        </style>
    </head>
    <body>
        <section class="aui-content">
            <ul class="aui-list aui-collapse aui-border">
                <div class="aui-collapse-item">
                    <div id="loan_content" class="aui-collapse-content" style="display:block;">
                        <li class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:30%; margin-right: 1rem">
                                    贷款机构：
                                </div>
                                <div class="aui-list-item-input" style="width:70%;">
                                    <select id="selectAgent" onchange="checkAgent()">
                                        <option value="" disabled selected>点我选择机构</option>
                                        <option>拍拍贷</option>
                                        <option>点融网</option>
                                        <option>你我贷</option>
                                        <option>中融投</option>
                                        <option>君融贷</option>
                                        <option>才米公社</option>
                                        <option>其他</option>
                                    </select>
                                </div>
                            </div>
                        </li>
                        <li id="otherAgent" class="aui-list-item" style="display:none">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:30%; margin-right: 1rem">
                                    其他机构：
                                </div>
                                <div class="aui-list-item-input" style="width:70%;">
                                    <input id="otherAgentText" type="text" placeholder="请在此输入其他贷款机构" value="">
                                </div>
                            </div>
                        </li>
                        <li class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:30%; margin-right: 1rem">
                                    总借款(元)：
                                </div>
                                <div class="aui-list-item-input" style="width:70%;">
                                    <input id="totalLoanText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:27%; margin-right: 1rem">
                                    还款方式：
                                </div>
                                <div class="aui-list-item-input" style="width:73%;">
                                    <label>
                                        <input class="aui-radio" type="radio" name="payType" checked onchange ="changePayType();">
                                        按月还款</label>
                                    <label>
                                        <input class="aui-radio" type="radio" name="payType" onchange ="changePayType();">
                                        随借随还</label>
                                </div>
                            </div>
                        </li>
                        <li id="monthPayMode" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:27%; margin-right: 1rem">
                                    月还方式：
                                </div>
                                <div class="aui-list-item-input" style="width:73%;">
                                    <label>
                                        <input class="aui-radio" type="radio" name="monthPayType" checked>
                                        等额本息</label>
                                    <label>
                                        <input class="aui-radio" type="radio" name="monthPayType">
                                        等额本金</label>
                                </div>
                            </div>
                        </li>
                        <li id="loanPeriod" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div id="loanPeriodLabel" class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    借款总期限(月)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="loanPeriodText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="monthRate" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    月利率(%)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="monthRateText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="monthFeeRate" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    月费率(%)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="monthFeeRateText" type="number" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="dayliRate" class="aui-list-item" style="display:none">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    日利率(%)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="dayliRateText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="dayliFeeRate" class="aui-list-item" style="display:none">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    日费率(%)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="dayliFeeRateText" type="number" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="oneTimeRate" class="aui-list-item" style="display:none">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    一次性费率(%)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="oneTimeRateText" type="number" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="serviceRate" class="aui-list-item" style="display:none">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:42%; margin-right: 1rem">
                                    其他费用(元)：
                                </div>
                                <div class="aui-list-item-input" style="width:58%;">
                                    <input id="serviceRateText" type="number" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li class="aui-list-item" style="display:block;">
                            <div class="aui-list-item-inner">
                                <div id="payDate" class="aui-list-item-label" style="width:25%; margin-right: 1rem">
                                    起始还款：
                                </div>
                                <div id="yearWrap" class="aui-list-item-input" style="width:15%; padding:0">
                                    <select id="selectYear" style="color: #000"  onchange="checkYear()">
                                        <option value="" disabled selected>[选择]</option>
                                        <option>2015</option>
                                        <option>2016</option>
                                        <option>2017</option>
                                        <option>2018</option>
                                        <option>2019</option>
                                        <option>2020</option>
                                    </select>
                                </div>
                                <div style="width:8%;  padding:0">
                                    年
                                </div>
                                <div id="monthWrap" class="aui-list-item-input" style="width:10%; padding:0">
                                    <select id="selectMonth" style="color: #000"  onchange="checkMonth()">
                                        <option value="" disabled selected>[选择]</option>
                                        <option>01</option>
                                        <option>02</option>
                                        <option>03</option>
                                        <option>04</option>
                                        <option>05</option>
                                        <option>06</option>
                                        <option>07</option>
                                        <option>08</option>
                                        <option>09</option>
                                        <option>10</option>
                                        <option>11</option>
                                        <option>12</option>
                                    </select>
                                </div>
                                <div style="width:8%;  padding:0">
                                    月
                                </div>
                                <div id="dayWrap" class="aui-list-item-input" style="width:10%; padding:0">
                                    <select id="selectDay" style="color: #000"  onchange="checkDay()">
                                        <option value="" disabled selected>[选择]</option>
                                        <option>01</option>
                                        <option>02</option>
                                        <option>03</option>
                                        <option>04</option>
                                        <option>05</option>
                                        <option>06</option>
                                        <option>07</option>
                                        <option>08</option>
                                        <option>09</option>
                                        <option>10</option>
                                        <option>11</option>
                                        <option>12</option>
                                        <option>13</option>
                                        <option>14</option>
                                        <option>15</option>
                                        <option>16</option>
                                        <option>17</option>
                                        <option>18</option>
                                        <option>19</option>
                                        <option>20</option>
                                        <option>21</option>
                                        <option>22</option>
                                        <option>23</option>
                                        <option>24</option>
                                        <option>25</option>
                                        <option>26</option>
                                        <option>27</option>
                                        <option>28</option>
                                        <option>29</option>
                                        <option>30</option>
                                        <option>31</option>
                                    </select>
                                </div>
                                <div style="width:8%;  padding:0">
                                    日
                                </div>
                            </div>
                        </li>
                        <li id="readOnlyLi" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:30%; margin-right: 1rem">
                                    总还款(元)：
                                </div>
                                <div class="aui-list-item-input" style="width:70%;">
                                    <input id="totalPayText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="monthPay" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:30%; margin-right: 1rem">
                                    月还款(元)：
                                </div>
                                <div class="aui-list-item-input" style="width:70%;">
                                    <input id="monthPayText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <li id="readOnlyLi" class="aui-list-item">
                            <div class="aui-list-item-inner">
                                <div class="aui-list-item-label" style="width:30%; margin-right: 1rem">
                                    已还款(元)：
                                </div>
                                <div class="aui-list-item-input" style="width:70%;">
                                    <input id="alreadyPayText" type="text" placeholder="请在此输入数字" value="">
                                </div>
                            </div>
                        </li>
                        <div id="pay_progresshint"style="width:100%; background-color:#fff; padding: 0.5rem 1rem 0.5rem 1rem">
                            <img class="quote_icon" style="width:0.7rem; margin-right: 0.5rem; display:inline;" src="../../image/main_frm_3/left_quote.png"></img>
                            <h6 style="width: 70%;display:inline;">还款进度仅供参考，根据您登记的贷款记录推算，准确前提是您按时还款。^_^</h6>
                            <img class="quote_icon" style="width:0.7rem; margin-left: 0.5rem; display:inline;" src="../../image/main_frm_3/right_quote.png"></img>
                        </div>
                        <li id="saveBtn" class="aui-list-item">
                            <div class="aui-list-item-inner aui-list-item-center aui-list-item-btn"  style="padding-top:0.5rem; padding-bottom:1rem">
                                <div class="aui-btn aui-btn-danger aui-margin-r-10" style="width:4.0rem;" tapmode onclick="deleteRecord()">
                                    删除
                                </div>
                                <div id="modifyBtn" class="aui-btn aui-btn-warning aui-margin-r-10" style="width:4.0rem;" tapmode onclick="modifyRecord()">
                                    修改
                                </div>
                                <div class="aui-btn aui-btn-info" style="width:4.0rem;" tapmode onclick="saveRecord()">
                                    保存
                                </div>
                            </div>
                        </li>
                    </div>
                </div>
            </ul>
        </section>
    </body>
    <script type="text/javascript" src="../../script/api.js" ></script>
    <script type="text/javascript" src="../../script/aui-tab.js" ></script>
    <script type="text/javascript" src="../../script/APICloud-rest-SHA1.js" ></script>
    <script type="text/javascript" src="../../script/aui-toast.js" ></script>
    <script type="text/javascript" src="../../script/aui-dialog.js" ></script>
    <script>
        var company_icons = ["paipai.jpg", "dianrong.jpg", "niwodai.jpg", "zhongrongtou.jpg", "junrongdai.jpg", "caimi.jpg"];
        var client = new Resource("A6928874653494", "4B6C428C-24C2-F9E7-3175-F983922E8C8F");
        var modelUserLoanRecord = client.Factory("userLoanRecord");
        var toast = new auiToast();
        var dialog = new auiDialog();
        function checkAgent() {
            var agent = document.getElementById("selectAgent");
            //定位id
            var index = agent.selectedIndex;
            // 选中索引
            var otherAgent = document.getElementById("otherAgent");
            if (agent.length == (index + 1)) {
                otherAgent.style.display = "block";
            } else {
                otherAgent.style.display = "none";
            }
        }

        function checkYear() {
            var agent = document.getElementById("selectYear");
            var yearWrap = document.getElementById("yearWrap");
            //定位id
            var index = agent.selectedIndex;
            if (index == 0) {
                agent.style.color = "#989898"
            } else {
                agent.style.color = "#000"
                yearWrap.style.width = "15%";
            }
        }

        function checkMonth() {
            var agent = document.getElementById("selectMonth");
            var monthWrap = document.getElementById("monthWrap");
            //定位id
            var index = agent.selectedIndex;
            if (index == 0) {
                agent.style.color = "#989898"
            } else {
                agent.style.color = "#000";
                monthWrap.style.width = "10%";
            }
        }

        function checkDay() {
            var agent = document.getElementById("selectDay");
            var dayWrap = document.getElementById("dayWrap");
            //定位id
            var index = agent.selectedIndex;
            if (index == 0) {
                agent.style.color = "#989898"
            } else {
                agent.style.color = "#000";
                dayWrap.style.width = "10%";
            }
        }

        function changePayType() {
            var radio = document.getElementsByName("payType");
            var radioLength = radio.length;
            for (var i = 0; i < radioLength; i++) {
                if (radio[i].checked) {
                    var payDate = document.getElementById("payDate");
                    var monthRate = document.getElementById("monthRate");
                    var dayliRate = document.getElementById("dayliRate");
                    var monthFeeRate = document.getElementById("monthFeeRate");
                    var dayliFeeRate = document.getElementById("dayliFeeRate");
                    var monthPay = document.getElementById("monthPay");
                    var monthPayMode = document.getElementById("monthPayMode");
                    var loanPeriodLabel = document.getElementById("loanPeriodLabel");
                    if (i == 0) {
                        monthRate.style.display = "block";
                        dayliRate.style.display = "none";
                        monthFeeRate.style.display = "block";
                        dayliFeeRate.style.display = "none";
                        monthPayMode.style.display = "block";
                        monthPay.style.display = "block";
                        loanPeriodLabel.textContent = "借款总期限(月)";
                        payDate.textContent = "起始还款：";
                    } else {
                        monthRate.style.display = "none";
                        dayliRate.style.display = "block";
                        monthFeeRate.style.display = "none";
                        dayliFeeRate.style.display = "block";
                        monthPayMode.style.display = "none";
                        monthPay.style.display = "none";
                        loanPeriodLabel.textContent = "借款总期限(天)";
                        payDate.textContent = "还款日期：";
                    }
                }
            }
        }

        function calcPayProgress(e) {
            var myDate = new Date();
            var year = parseInt(myDate.getFullYear());
            var month = parseInt(myDate.getMonth()) + 1;
            var day = parseInt(myDate.getDate());
            var oldYear = parseInt(e.validYear);
            var oldMonth = parseInt(e.validMonth);
            var oldDay = parseInt(e.validDay);
            if (e.payTypeIndex == 1) {
                var curDayCount = year * 365 + (month - 1) * 30 + day;
                var payDayCount = oldYear * 365 + (oldMonth - 1) * 30 + oldDay;
                if (curDayCount > payDayCount) {
                    return 100.00;
                } else {
                    return 0;
                }
            } else {
                var payMonth = 0;
                yearDiff = year - oldYear;
                if (yearDiff > 0) {
                    payMonth = (yearDiff - 1) * 12 + (12 - oldMonth + 1) + (month - 1);
                } else if (yearDiff == 0) {
                    //alert("Date: " + year + " " + month + " " + day);
                    monthDiff = month - oldMonth;
                    if (monthDiff > 0) {
                        payMonth = monthDiff;
                    } else if (monthDiff < 0) {
                        return 0;
                    }
                } else if (yearDiff < 0) {
                    return 0;
                }
                if (day > oldDay) {
                    payMonth = payMonth + 1;
                }
                var PayRate = payMonth * 100.00 / e.loanPeriod;
                if (PayRate > 100.00) {
                    PayRate = 100.00;
                }
                return PayRate;
            }
            //alert("Date: " + oldYear + " " + oldMonth + " " + oldDay);
        }

        function loadLoanDetail() {
            toast.loading({
                title : "加载中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            modelUserLoanRecord.query({
                filter : {
                    "limit" : 1,
                    "where" : {
                        "id" : api.pageParam.index,
                    }
                }
            }, function(ret, err) {
                if (err) {
                    //处理错误 err
                    alert(JSON.stringify(err));
                } else {
                    //处理数据 ret
                    //alert(JSON.stringify(ret));
                    var otherAgentText = document.getElementById("otherAgentText");
                    var totalLoanText = document.getElementById("totalLoanText");
                    var loanPeriodText = document.getElementById("loanPeriodText");
                    var monthRateText = document.getElementById("monthRateText");
                    var dayliRateText = document.getElementById("dayliRateText");
                    var monthFeeRateText = document.getElementById("monthFeeRateText");
                    var dayliFeeRateText = document.getElementById("dayliFeeRateText");
                    var serviceRateText = document.getElementById("serviceRateText");
                    var oneTimeRateText = document.getElementById("oneTimeRateText");
                    var totalPayText = document.getElementById("totalPayText");
                    var alreadyPayText = document.getElementById("alreadyPayText");
                    var monthPayText = document.getElementById("monthPayText");
                    if (isNaN(parseInt(ret[0].monthFeeRate))) {
                        ret[0].monthFeeRate = 0;
                    }
                    if (isNaN(parseInt(ret[0].dayFeeRate))) {
                        ret[0].dayFeeRate = 0
                    }
                    if (isNaN(parseInt(ret[0].otherExpense))) {
                        ret[0].otherExpense = 0;
                    }
                    if (isNaN(parseFloat(ret[0].oneTimeRate))) {
                        ret[0].oneTimeRate = 0
                    }
                    otherAgentText.value = ret[0].finInstName;
                    totalLoanText.value = ret[0].loanAmount;
                    monthRateText.value = ret[0].monthRate;
                    dayliRateText.value = ret[0].dayRate;
                    monthFeeRateText.value = ret[0].monthFeeRate;
                    dayliFeeRateText.value = ret[0].dayFeeRate;
                    var totalPayNum = ret[0].loanAmount;
                    if (ret[0].payTypeIndex == 0) {
                        totalPayNum = totalPayNum * (ret[0].monthRate + ret[0].monthFeeRate) * ret[0].loanPeriod * 0.01 + totalPayNum;
                    } else if (ret[0].payTypeIndex == 1) {
                        totalPayNum = totalPayNum * (ret[0].dayRate + ret[0].dayFeeRate) * ret[0].loanPeriod * 0.01 + totalPayNum;
                    }
                    totalPayNum = totalPayNum + ret[0].otherExpense;
                    totalPayNum = totalPayNum + ret[0].oneTimeRate * ret[0].loanAmount * 0.01;
                    monthPayText.value = (totalPayNum / ret[0].loanPeriod).toFixed(2);
                    totalPayText.value = totalPayNum.toFixed(2);
                    var calcRate = calcPayProgress(ret[0]);
                    alreadyPayText.value = (totalPayText.value * calcRate * 0.01).toFixed(2);
                    loanPeriodText.value = ret[0].loanPeriod;
                    serviceRateText.value = ret[0].otherExpense;
                    oneTimeRateText.value = ret[0].oneTimeRate;
                    var selectAgent = document.getElementById("selectAgent");
                    var selectYear = document.getElementById("selectYear");
                    var selectMonth = document.getElementById("selectMonth");
                    var selectDay = document.getElementById("selectDay");
                    var agentIndex = ret[0].finInstIndex ? ret[0].finInstIndex : selectAgent.options.length - 1;
                    //alert(agentIndex);
                    selectAgent.options[agentIndex].selected = true;
                    selectYear.options[ret[0].validYear - parseInt(selectYear.options[1].text) + 1].selected = true;
                    selectMonth.options[ret[0].validMonth].selected = true;
                    selectDay.options[ret[0].validDay].selected = true;
                    var payType = document.getElementsByName("payType");
                    var monthPayType = document.getElementsByName("monthPayType");
                    for (var i = 0; i < payType.length; i++) {
                        payType[i].removeAttribute('checked');
                    }
                    for (var i = 0; i < monthPayType.length; i++) {
                        monthPayType[i].removeAttribute('checked');
                    }
                    payType[ret[0].payTypeIndex].setAttribute('checked', 'true');
                    monthPayType[ret[0].monthlyPayTypeIndex].setAttribute('checked', 'true');
                    checkAgent();
                    changePayType();
                    disableEdit();
                    toast.hide();
                }
            });
        }

        function disableEdit() {
            var list_items = document.querySelectorAll("li");
            var inputs = document.querySelectorAll("input[type=text]");
            var radioes = document.querySelectorAll("input[type=radio]");
            var checked = document.querySelectorAll("input:checked");
            var selects = document.querySelectorAll("select");
            for (var i = 0; i < inputs.length; i++) {
                inputs[i].setAttribute('readOnly', 'true');
            }
            for (var i = 0; i < radioes.length; i++) {
                radioes[i].style.display = "none";
                radioes[i].parentNode.style.display = "none";
            }
            for (var i = 0; i < checked.length; i++) {
                checked[i].style.display = "inline-block";
                checked[i].setAttribute('disabled', 'disabled');
                checked[i].parentNode.style.display = "inline-block";
            }
            for (var i = 0; i < selects.length; i++) {
                selects[i].setAttribute('disabled', 'disabled');
            }
            for (var i = 0; i < list_items.length; i++) {
                if (list_items[i].id == "saveBtn") {
                    break;
                }
                list_items[i].style.backgroundColor = "#fff";
            }
            //document.getElementById("select_agent").removeAttribute('disabled');
            //document.getElementById("select_agent").setAttribute('disabled', 'disabled');
        }

        function modifyRecord() {
            var modifyBtn = document.getElementById("modifyBtn");
            if (modifyBtn.textContent == "取消") {
                api.closeWin({
                    name : 'my_loan_win'
                });
            } else {
                modifyBtn.textContent = "取消"
            }
            var list_items = document.querySelectorAll("li");
            var inputs = document.querySelectorAll("input[type=text]");
            var radioes = document.querySelectorAll("input[type=radio]");
            var checked = document.querySelectorAll("input:checked");
            var selects = document.querySelectorAll("select");
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].id != "totalPayText" && inputs[i].id != "alreadyPayText")
                    inputs[i].removeAttribute('readOnly');
            }
            for (var i = 0; i < radioes.length; i++) {
                radioes[i].style.display = "inline-block";
                radioes[i].parentNode.style.display = "inline-block";
            }
            for (var i = 0; i < checked.length; i++) {
                checked[i].style.display = "inline-block";
                checked[i].removeAttribute('disabled');
                checked[i].parentNode.style.display = "inline-block";
            }
            for (var i = 0; i < selects.length; i++) {
                selects[i].removeAttribute('disabled');
            }
            for (var i = 0; i < list_items.length; i++) {
                if (list_items[i].id == "readOnlyLi" || list_items[i].id == "saveBtn" || list_items[i].id == "monthPay") {
                    continue;
                }
                list_items[i].style.backgroundColor = "#f5f5f5";
            }
        }

        function saveRecord() {
            var modifyBtn = document.getElementById("modifyBtn");
            if (modifyBtn.textContent == "修改") {
                return;
            } else {
                modifyBtn.textContent = "修改";
            }
            var loanInfo = {
                totalLoan : 0,
                agent : "",
                agentIndex : 0,
                loanItemId : ""
            };
            var selectAgent = document.getElementById("selectAgent");
            if (selectAgent.length == (selectAgent.selectedIndex + 1)) {
                loanInfo.agent = document.getElementById("otherAgentText").value;
                loanInfo.agentIndex = 0;
            } else {
                loanInfo.agent = selectAgent.options[selectAgent.selectedIndex].text;
                loanInfo.agentIndex = selectAgent.selectedIndex;
            }
            if (!selectAgent.selectedIndex) {
                dialog.alert({
                    title : "贷款机构未输入",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            loanInfo.totalLoan = document.getElementById("totalLoanText").value;
            if (isNaN(parseInt(loanInfo.totalLoan))) {
                dialog.alert({
                    title : "贷款数额错误",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            var payTypeIndex = 0;
            var radio = document.getElementsByName("payType");
            var radioLength = radio.length;
            for (var i = 0; i < radioLength; i++) {
                if (radio[i].checked) {
                    payTypeIndex = i;
                }
            }
            var monthPayTypeIndex = 0;
            radio = document.getElementsByName("monthPayType");
            radioLength = radio.length;
            for (var i = 0; i < radioLength; i++) {
                if (radio[i].checked) {
                    monthPayTypeIndex = i;
                }
            }
            var loanPeriodText = document.getElementById("loanPeriodText").value;
            if (isNaN(parseInt(loanPeriodText))) {
                dialog.alert({
                    title : "贷款期限错误",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            var monthRateText = document.getElementById("monthRateText").value;
            if ((payTypeIndex == 0) && isNaN(parseFloat(monthRateText))) {
                dialog.alert({
                    title : "月利率错误",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            var dayliRateText = document.getElementById("dayliRateText").value;
            if ((payTypeIndex == 1) && isNaN(parseFloat(dayliRateText))) {
                dialog.alert({
                    title : "日利率错误",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            var monthFeeRateText = document.getElementById("monthFeeRateText").value;
            if (isNaN(parseInt(monthFeeRateText))) {
                monthFeeRateText = 0;
            }
            var dayliFeeRateText = document.getElementById("dayliFeeRateText").value;
            if (isNaN(parseInt(dayliFeeRateText))) {
                dayliFeeRateText = 0;
            }
            var serviceRateText = document.getElementById("serviceRateText").value;
            if (isNaN(parseInt(loanPeriodText))) {
                serviceRateText = 0;
            }
            var oneTimeRateText = document.getElementById("oneTimeRateText").value;
            if (isNaN(parseFloat(loanPeriodText))) {
                oneTimeRateText = 0
            }
            var selectYear = document.getElementById("selectYear").value;
            if (isNaN(parseInt(selectYear))) {
                dialog.alert({
                    title : "年份未选择",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            var selectMonth = document.getElementById("selectMonth").value;
            if (isNaN(parseInt(selectMonth))) {
                dialog.alert({
                    title : "月份未选择",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            var selectDay = document.getElementById("selectDay").value;
            if (isNaN(parseInt(selectDay))) {
                dialog.alert({
                    title : "日期未选择",
                    msg : "请重新输入 ^_^",
                    buttons : ['确定']
                });
                return;
            }
            /*
             var totalPayNum = ret[0].loanAmount;
             if (ret[0].payTypeIndex == 0) {
             totalPayNum = totalPayNum * ret[0].monthRate * ret[0].loanPeriod * 0.01 + totalPayNum;
             } else if (ret[0].payTypeIndex == 1) {
             totalPayNum = totalPayNum * ret[0].dayRate * ret[0].loanPeriod * 0.01 + totalPayNum;
             }
             totalPayNum = totalPayNum + ret[0].otherExpense;
             totalPayNum = totalPayNum + ret[0].oneTimeRate * ret[0].loanAmount * 0.01;
             */
            toast.loading({
                title : "保存中",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            modelUserLoanRecord.save({
                "_id" : api.pageParam.index
            }, {
                "$set" : {
                    "finInstName" : loanInfo.agent,
                    "finInstIndex" : loanInfo.agentIndex,
                    "loanAmount" : loanInfo.totalLoan,
                    "payTypeIndex" : payTypeIndex,
                    "monthlyPayTypeIndex" : monthPayTypeIndex,
                    "loanPeriod" : loanPeriodText,
                    "monthRate" : monthRateText,
                    "dayRate" : dayliRateText,
                    "monthFeeRate" : monthFeeRateText,
                    "dayFeeRate" : dayliFeeRateText,
                    "oneTimeRate" : oneTimeRateText,
                    "otherExpense" : serviceRateText,
                    "validYear" : selectYear,
                    "validMonth" : selectMonth,
                    "validDay" : selectDay,
                    "loanItemIconName" : '../../image/company_icon/' + company_icons[selectAgent.selectedIndex - 1],
                    "payProgress" : 20,
                    "banAlertMonth" : 0,
                }
            }, function(ret, err) {
                if (err) {
                    //处理错误 err
                } else {
                    //处理数据 ret
                    //alert(JSON.stringify(ret));
                    var totalPayText = document.getElementById("totalPayText");
                    var alreadyPayText = document.getElementById("alreadyPayText");
                    var monthPayText = document.getElementById("monthPayText");
                    var totalPayNum = ret.loanAmount;
                    if (ret.payTypeIndex == 0) {
                        totalPayNum = totalPayNum * (ret.monthRate + ret.monthFeeRate) * ret.loanPeriod * 0.01 + totalPayNum;
                    } else if (ret.payTypeIndex == 1) {
                        totalPayNum = totalPayNum * (ret.dayRate + ret.dayFeeRate) * ret.loanPeriod * 0.01 + totalPayNum;
                    }
                    totalPayNum = totalPayNum + ret.otherExpense;
                    totalPayNum = totalPayNum + ret.oneTimeRate * ret.loanAmount * 0.01;
                    totalPayText.value = totalPayNum;
                    monthPayText.value = (totalPayNum / ret.loanPeriod).toFixed(2);
                    alreadyPayText.value = (totalPayText.value * calcPayProgress(ret) * 0.01).toFixed(2);
                    toast.hide();
                    toast.success({
                        title : "保存成功！",
                        duration : 1000
                    });
                    disableEdit();
                }
            })
        }

        function deleteRecord() {
            toast.loading({
                title : "正在删除",
                duration : 2000
            }, function(ret) {
                console.log(ret);
                setTimeout(function() {
                    toast.hide();
                }, 30000)
            });
            modelUserLoanRecord.delete({
                "_id" : api.pageParam.index
            }, function(ret, err) {
                if (err) {
                    //处理错误 err
                } else {
                    //处理数据 ret
                    toast.hide();
                    api.sendEvent({
                        name : 'ev_deleteLoanItem',
                        extra : {
                            index : api.pageParam.index,
                        }
                    });
                }
            })
        }

        apiready = function() {
            api.setRefreshHeaderInfo({
                visible : true,
                bgColor : '#ccc',
                textColor : '#fff',
                textDown : '下拉刷新...',
                textUp : '松开刷新...',
                showTime : true
            }, function(ret, err) {
                api.refreshHeaderLoadDone();
            });
            api.parseTapmode();
            loadLoanDetail();
        }
    </script>
</html>